﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using NetTaste;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Reflection;
using System.Text.RegularExpressions;
using VM.Common.DataUntil;
using VM.Common.Encryption5Until;
using VM.Common.Result;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.User;
using VM.IBLL.Token;
using VM.IBLL.User;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{



    [Route("api/user")]
    [ApiController]
    public class UserController : Controller
    {
        private IJWTService _IJWTService = null;
        //private readonly ILogger<AuthController> _logger;
        private readonly ITokenBLL _ITokenInterface;
        private readonly IUserBLL _IUserBLL;
        public UserController(IJWTService iJWTService, ITokenBLL tokenInterface, IUserBLL iUserBLL)
        {
            _IJWTService = iJWTService;
            //_logger = logger;
            _ITokenInterface = tokenInterface;
            _IUserBLL = iUserBLL;

        }

        /// <summary>
        /// 登录接口 PC
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("login")]
        public async Task< ApiDataResult<LoginResDataEntity>> login(UserLoginEntity obj)
        {

           string s= MD5ED.MD5Encrypt32(obj.password);
            ApiDataResult<LoginResDataEntity> LoginRes = _IUserBLL.loginAsync(obj);

            if (LoginRes.code == 200)
            {
                string AESUserId = LoginRes.data.id;
                JsonObject jo = await OAuthController.GetJwtTokenAsync(AESUserId, obj.publicKey, obj.privateKey);
                string accessToken = jo.token;
                string refreshToken = jo.refreshtoken;
                string tokenType = "Bearer";
                if (jo.code == 200)
                {
                    LoginRes.msg = "登录成功";
                    LoginRes.data.access_token = accessToken;
                    LoginRes.data.refresh_token = refreshToken;
                    LoginRes.data.token_type = tokenType;

                    LoginRes.data.id = _IJWTService.AnaliseToken(LoginRes.data.access_token, "id");
                    LoginRes.data.exp = _IJWTService.AnaliseToken(LoginRes.data.access_token, "exp");
                    LoginRes.data.nbf = _IJWTService.AnaliseToken(LoginRes.data.access_token, "nbf");
                    return LoginRes;
                }
                else
                {
                    return new ApiDataResult<LoginResDataEntity> {
                       code=403,
                       msg="鉴权时失败",
                       data=null
                    };
                } 
            }
            else
            {

                return LoginRes;
            }
        }

        /// <summary>
        /// 退出登录接口 PC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("logout")]
        public ApiResult logout()
        {
            ApiResult joRes = _IUserBLL.logout();
            return joRes;
        }


        /// <summary>
        /// 获取用户信息 PC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("userInfo")]
        public ApiDataResult<UserInfoEntity> userInfo()
        {
            ApiDataResult<UserInfoEntity> joRes = _IUserBLL.userinfo();
            return joRes;
        }


       
 
         }
}
